package com.daxia.ogmall.service;

import java.util.ArrayList;
import java.util.List;

import org.apache.commons.collections.CollectionUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.daxia.ogmall.dao.ValidateCodeDAO;
import com.daxia.ogmall.dto.ValidateCodeDTO;
import com.daxia.ogmall.model.ValidateCode;
import com.daxia.ogmall.support.Page;
import com.daxia.ogmall.util.BeanMapper;

@Service
public class ValidateCodeService {
	
	@Autowired
	private ValidateCodeDAO validateCodeDAO;
	
	public List<ValidateCodeDTO> find(ValidateCodeDTO query, Page page) {
		List<ValidateCode> models = validateCodeDAO.find(query, page);
		List<ValidateCodeDTO> dtos = toDTOs(models);
		return dtos;
	}
	
	public List<ValidateCodeDTO> findAll() {
		return this.find(new ValidateCodeDTO(), null);
	}
	
	public List<ValidateCodeDTO> find(int count) {
		Page page = new Page();
		page.setPage(1);
		page.setPageSize(count);
		return this.find(new ValidateCodeDTO(), page);
	}
	
	public Long create(ValidateCodeDTO dto) {
		ValidateCode model = new ValidateCode();
		toModel(model, dto);
		return validateCodeDAO.create(model);
	}
	
	public ValidateCodeDTO load(Long id) {
	    ValidateCode model = validateCodeDAO.load(id);
	    return toDTO(model);
    }

	public void updateAllFields(ValidateCodeDTO dto) {
		ValidateCode model = validateCodeDAO.load(dto.getId());
		toModel(model, dto);
		validateCodeDAO.update(model);
    }

	public void deleteByIds(Long[] ids) {
		if (ids != null) {
			for (Long id : ids) {
				//validateCodeDAO.deleteById(id);
				validateCodeDAO.deleteByIdPhysical(id);
			}
		}
    }
	
	public void deleteById(Long id) {
		this.deleteByIds(new Long[] {id});
	}

	public ValidateCodeDTO findOne(ValidateCodeDTO query) {
		ValidateCode model = validateCodeDAO.findOne(query);
		return toDTO(model);
	}
	
	private List<ValidateCodeDTO> toDTOs(List<ValidateCode> models) {
		if (CollectionUtils.isEmpty(models)) {
			return new ArrayList<ValidateCodeDTO>(0);
		}
		List<ValidateCodeDTO> dtos = new ArrayList<ValidateCodeDTO>(models.size());
		for (ValidateCode model : models) {
	        ValidateCodeDTO dto = toDTO(model);
	        dtos.add(dto);
        }
	    return dtos;
    }
    
    private ValidateCodeDTO toDTO(ValidateCode model) {
		if (model == null) {
			return null;
		}
		ValidateCodeDTO dto = BeanMapper.map(model, ValidateCodeDTO.class);
		
		return dto;
	}
	
	private void toModel(ValidateCode model, ValidateCodeDTO dto) {
		BeanMapper.copy(dto, model);
    }
	
	@SuppressWarnings("unused")
	private List<ValidateCode> toModels(List<ValidateCodeDTO> dtos) {
		if (CollectionUtils.isEmpty(dtos)) {
			return new ArrayList<ValidateCode>(0);
		}
		List<ValidateCode> models = new ArrayList<ValidateCode>(dtos.size());
		for (ValidateCodeDTO dto : dtos) {
	        ValidateCode model = new ValidateCode();
	        toModel(model, dto);
	        models.add(model);
        }
		return models;
	}

	public ValidateCodeDTO findByMobile(String mobile) {
		ValidateCodeDTO query = new ValidateCodeDTO();
		query.setMobile(mobile);;
		return findOne(query);
	}
	
	public void createOrReplace(String mobile, String validateCode, Integer type) {
	    ValidateCodeDTO dto = this.findByMobile(mobile);
	    if (dto == null) {
	        dto = new ValidateCodeDTO();
	        dto.setMobile(mobile);
	        dto.setValidateCode(validateCode);
            dto.setTime(System.currentTimeMillis());
            this.create(dto);
	    } else {
	        dto.setValidateCode(validateCode);
	        dto.setTime(System.currentTimeMillis());
	        this.updateAllFields(dto);
	    }
	}
	
}
